<template>
  <a-modal
    :title="model && model.id ? '编辑供应商&经销商' : '新建供应商&经销商'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    okText=""
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item label="名称">
          <a-input
            v-decorator="['name', { initialValue: model?.name, rules: [{ required: true,message: '请输入名称' }] }]"
            placeholder="请输入名称"
          />
        </a-form-item>
        <a-form-item label="类型">
          <a-select v-decorator="['types', { initialValue: '1',rules: [{ required: true,message: '请选择类型' }] }]" placeholder="请选择" @select="change">
            <a-select-option value="1">供应商</a-select-option>
            <a-select-option value="2">经销商</a-select-option>
            <a-select-option value="3">供应商&经销商</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="省" v-if="modelTypes == '2' || modelTypes == '3'">
          <a-input v-decorator="['province', { initialValue: model?.province }]" placeholder="请输入省" />
        </a-form-item>
        <a-form-item label="市" v-if="modelTypes == '2' || modelTypes == '3'">
          <a-input v-decorator="['city', { initialValue: model?.city }]" placeholder="请输入市" />
        </a-form-item>
        <a-form-item label="区域" v-if="modelTypes == '2' || modelTypes == '3'">
          <a-select v-decorator="['area', { rules: [{ required: true, message: '请选择区域' }] }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.option" :key="i.value">{{ i.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="业务负责人" v-if="modelTypes == '2' || modelTypes == '3'">
          <a-select v-decorator="['empId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in userOption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label=" "
          :colon="false"
          v-if="modelTypes == '2' || modelTypes == '3'"
        >
          <a-checkbox v-decorator="['enabled', { initialValue: model?.enabled }]" v-model="enabled">
            启用账期
          </a-checkbox>
        </a-form-item>
        <a-form-item
          label="账期额度"
          v-if="
            (enabled === true) ||((modelTypes == '2' || modelTypes == '3') && enabled === true)
          "
        >
          <a-input
            addonAfter="元"
            v-decorator="['accountQuota', { initialValue: model?.accountQuota }]"
            placeholder="请输入账期额度"
          />
        </a-form-item>
        <a-form-item
          label="账期天数"
          v-if="
            (enabled === true ) ||((modelTypes == '2' || modelTypes == '3') && enabled === true)
          "
        >
          <a-input
            addonAfter="天"
            v-decorator="['accountDates', { initialValue: model?.accountDates }]"
            placeholder="请输入账期天数"
          />
        </a-form-item>
        <a-form-item
          label="超额处理"
          v-if="(enabled === true) || ((modelTypes == '2' || modelTypes == '3') && enabled === true)">
          <a-select v-decorator="['handle', { initialValue: model?.handle }]" placeholder="请选择">
            <a-select-option value="1">超出额度提示</a-select-option>
            <a-select-option value="2">超出额度禁止出库</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea rows="2" v-decorator="['memo', { initialValue: model?.memo }]" placeholder="请输入备注" />
        </a-form-item>
        <a-form-item label="状态" v-if="model && model.id > 0">
          <a-select v-decorator="['status', { initialValue: model?.status }]" placeholder="请选择">
            <a-select-option value="1">合作中</a-select-option>
            <a-select-option value="2">停止合作</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getAreaListUsingPOST } from '@/api/api/xitongguanli.ts'
import { getEmployeesListUsingPOST } from '@/api/api/yuangongguanli.ts'
// 表单字段
const fields = ['name','area', 'types', 'empId','province', 'city', 'enabled','accountQuota', 'accountDates','handle', 'memo', 'status', 'id']
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      userOption: [],
      queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      option: [],
      modelTypes: '',
      form: this.$form.createForm(this),
      enabled: false,
    }
  },
  created() {
    this.getSearch()
    this.getUserOption()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
      this.modelTypes = this.model.types
    })
  },
  methods: {
    change(e) {
      this.modelTypes = e
    },
    getUserOption() {
      getEmployeesListUsingPOST(this.optionParam)
        .then((res) => {
          this.userOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getSearch() {
      getAreaListUsingPOST(this.queryParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
